package qldc.pm.tiensu.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import qldc.pm.tiensu.NoSuchLichSuXoaAnTichException;
import qldc.pm.tiensu.dto.LichSuXoaAnTichDTO;
import qldc.pm.tiensu.model.DoiTuongTienSu;
import qldc.pm.tiensu.model.LichSuXoaAnTich;
import qldc.pm.tiensu.model.TienSu;
import qldc.pm.tiensu.model.impl.LichSuXoaAnTichImpl;
import qldc.pm.tiensu.service.base.LichSuXoaAnTichLocalServiceBaseImpl;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.User;

/**
 * The implementation of the lich su xoa an tich local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are
 * added, rerun ServiceBuilder to copy their definitions into the
 * {@link qldc.pm.tiensu.service.LichSuXoaAnTichLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security
 * checks based on the propagated JAAS credentials because this service can only
 * be accessed from within the same VM.
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @see qldc.pm.tiensu.service.base.LichSuXoaAnTichLocalServiceBaseImpl
 * @see qldc.pm.tiensu.service.LichSuXoaAnTichLocalServiceUtil
 */
public class LichSuXoaAnTichLocalServiceImpl extends
		LichSuXoaAnTichLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never reference this interface directly. Always use {@link
	 * qldc.pm.tiensu.service.LichSuXoaAnTichLocalServiceUtil} to access the
	 * lich su xoa an tich local service.
	 */
	public LichSuXoaAnTich addHoSoXoaAnTich(LichSuXoaAnTichDTO dto, long userId) throws SystemException {
		LichSuXoaAnTich lichSuXoaAnTich;

		if (dto.getLichSuXoaAnTichId() > 0) {
			try {
				lichSuXoaAnTich = fetchLichSuXoaAnTich(dto.getLichSuXoaAnTichId());
			} catch (SystemException e) {
				lichSuXoaAnTich = new LichSuXoaAnTichImpl();
				lichSuXoaAnTich.setNew(true);
			}
		} else {
	    	long lichSuXoaAnTichId = counterLocalService.increment(LichSuXoaAnTich.class.getName());
	    	lichSuXoaAnTich = createLichSuXoaAnTich(lichSuXoaAnTichId);
		}

		lichSuXoaAnTich.setCoQuanThiHanhAn(dto.getCoQuanThiHanhAn());
		lichSuXoaAnTich.setHinhPhat(dto.getHinhPhat());
		lichSuXoaAnTich.setHoVaTenDoiTuongTienSu(dto.getHoVaTenDoiTuongTienSu());
		lichSuXoaAnTich.setKinhGui(dto.getKinhGui());
		lichSuXoaAnTich.setMaHoSoXoaAnTich(dto.getMaHoSoXoaAnTich());
		lichSuXoaAnTich.setNgayKyDuyetXoaAnTich(dto.getNgayKyDuyetXoaAnTich());
		lichSuXoaAnTich.setNgayQuyetDinhThiHanhAn(dto.getNgayQuyetDinhThiHanhAn());
		lichSuXoaAnTich.setNgayYeuCauXoaAnTich(dto.getNgayYeuCauXoaAnTich());
		lichSuXoaAnTich.setNguoiYeuCauXoaAnTich(dto.getNguoiYeuCauXoaAnTich());
		lichSuXoaAnTich.setNguoiYeuCauXoaAnTich(dto.getNguoiYeuCauXoaAnTich());
		lichSuXoaAnTich.setSoQuyetDinhThiHanhAn(dto.getSoQuyetDinhThiHanhAn());
		lichSuXoaAnTich.setTienSuId(dto.getTienSuId());

		lichSuXoaAnTich.setNguoiLapHoSoXoaAnTichId(userId);
		lichSuXoaAnTich.setNgayLapHoSoXoaAnTich(new Date());

		return updateLichSuXoaAnTich(lichSuXoaAnTich);
	}

	public void thucHienXoaAnTich(long lichSuXoaAnTichId, User user) throws PortalException, SystemException {
		if (lichSuXoaAnTichId > 0 ) {
			LichSuXoaAnTich ls = fetchLichSuXoaAnTich(lichSuXoaAnTichId);

			if (Validator.isNotNull(ls)) {
				TienSu tienSu = tienSuLocalService.updateXoaAnTich(user, ls.getTienSuId(), true);

				List<TienSu> tienSus = tienSuLocalService.findByDoiTuongTienSuId(tienSu.getDoiTuongTienSuId());

				boolean xoaAnTichForDoiTuong = true;
				for (TienSu ts : tienSus) {
					if(!ts.isXoaAnTich()) {
						xoaAnTichForDoiTuong = false;
					}
				}

				if(xoaAnTichForDoiTuong) {
					DoiTuongTienSu doiTuongTienSu = doiTuongTienSuLocalService.findByDoiTuongTienSuId(tienSu.getDoiTuongTienSuId());
					doiTuongTienSu.setUserId(user.getUserId());
					doiTuongTienSu.setModifiedDate(new Date());
					doiTuongTienSu.setXoaAnTich(true);

					doiTuongTienSuLocalService.updateDoiTuongTienSu(doiTuongTienSu);
				}

				ls.setNgayKyDuyetXoaAnTich(new Date());
				ls.setNguoiThucHienXoaAnTichTrongDuLieuID(user.getUserId());

				updateLichSuXoaAnTich(ls);
			}
		}
	}

	public List<LichSuXoaAnTich> findByHoTen_MaSoHoSo_NgayLapHoSo_TrangThai(String hoVaTenDoiTuongTienSu, String maHoSoXoaAnTich, Date ngayYeuCauXoaAnTich, int trangThaiXuLyHoSoXoaAnTich, int start, int end, OrderByComparator order) {
		try {
			return lichSuXoaAnTichFinder.findByHoTen_MaSoHoSo_NgayLapHoSo_TrangThai(hoVaTenDoiTuongTienSu, maHoSoXoaAnTich, ngayYeuCauXoaAnTich, trangThaiXuLyHoSoXoaAnTich, start, end, order);
		} catch (SystemException e) {
			return new ArrayList<LichSuXoaAnTich>();
		}
	}

	public int countByHoTen_MaSoHoSo_NgayLapHoSo_TrangThai(String hoVaTenDoiTuongTienSu, String maHoSoXoaAnTich, Date ngayYeuCauXoaAnTich, int trangThaiXuLyHoSoXoaAnTich) {
		try {
			return lichSuXoaAnTichFinder.countByHoTen_MaSoHoSo_NgayLapHoSo_TrangThai(hoVaTenDoiTuongTienSu, maHoSoXoaAnTich, ngayYeuCauXoaAnTich, trangThaiXuLyHoSoXoaAnTich);
		} catch (SystemException e) {
			return 0;
		}
	}

	public LichSuXoaAnTich findByTienSuId(long tienSuId) {
		try {
			return lichSuXoaAnTichPersistence.findByTienSuId(tienSuId);
		} catch (NoSuchLichSuXoaAnTichException e) {
			return null;
		} catch (SystemException e) {
			return null;
		}
	}
	
	public List<Date> findByNgayKyDuyetXoaAnTich(Date tuNgayXoaAnTich, Date denNgayXoaAnTich, long doiTuongTienSuId) {
		try {
			return lichSuXoaAnTichFinder.findByNgayKyDuyetXoaAnTich(tuNgayXoaAnTich, denNgayXoaAnTich, doiTuongTienSuId);
		} catch (Exception e) {
			return null;
		}
	}

	public boolean kiemTraQuyetKyDuyetHoSoXoaAnTich(LichSuXoaAnTich lichSuXoaAnTich, long userId) {
		if (Validator.isNull(lichSuXoaAnTich.getNgayKyDuyetXoaAnTich()) && lichSuXoaAnTich.getNguoiThucHienXoaAnTichTrongDuLieuID() <= 0) {
			return true;
		}

		return false;
	}
}
